//
//  DZZDiscoverViewController.m
//  DZZTravel
//
//  Created by xalo on 16/7/13.
//  Copyright © 2016年 Eason. All rights reserved.
//

#import "DZZDiscoverViewController.h"
#import "RequestManager.h"
#import "DiscoverModel.h"
#import "DZZDisInfoController.h"
#import "TopView.h"

#define discoverUrl @"http://apiphp.yaochufa.com/portal/theme/areaThemeList?=&=&=2946BE8C-7613-46B9-9BA6-32D53FE58F2F&lang=app&machineCode=2946BE8C-7613-46B9-9BA6-32D53FE58F2F&machineType=0&pageIndex=1&pageSize=10&system=ios&version=5.2.3"
@interface DZZDiscoverViewController ()<UITableViewDelegate, UITableViewDataSource>


@property (nonatomic, strong)UITableView *tableView;
//存放数据
@property (nonatomic, strong)NSMutableArray *dataSource;
@end

@implementation DZZDiscoverViewController


- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kheight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = [UIColor clearColor];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addNavigationBarBution];
    //tableView的相关操作
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"DZZDiscoverCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    [self requestData];
}
#pragma mark ------请求数据-------

- (void)requestData {
  
    [RequestManager fectDataWithUrl:discoverUrl parameter:@{} method:@"GET" compelet:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

       
        for (NSDictionary *smallDic in bigDic[@"content"][@"themeList"]) {
            
            DiscoverModel *model = [[DiscoverModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.dataSource addObject:model];
        }

    } upDateUI:^{
        [self.tableView reloadData];
    }];
    

}

#pragma mark ------UITableViewDelegate-------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    DiscoverModel *model = self.dataSource[indexPath.row];
    DZZDiscoverCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    [cell.picView sd_setImageWithURL:[NSURL URLWithString:model.imageUrl]];
    cell.titlelab.text = model.title;
    cell.introLab.text = model.subTitle;
    
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    NSArray *visibleCells = [self.tableView visibleCells];
    for (DZZDiscoverCell *cell in visibleCells) {
        [cell cellOnTableView:self.tableView didScrollOnView:self.view];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 270;
}
//cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
    
 
    DZZDisInfoController *DIC = [[DZZDisInfoController alloc]init];
    
    self.tabBarController.tabBar.hidden = YES;
    //传数据模型，点击下一个界面信息
    DIC.discoverModel = self.dataSource[indexPath.row];
   
    [self.navigationController pushViewController:DIC animated:YES];
}


//在navigationBar添加按钮
- (void)addNavigationBarBution {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"西安📍" style:UIBarButtonSystemItemDone target:self action:@selector(locationAction)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"🔍去哪？" style:UIBarButtonSystemItemDone target:self action:@selector(whereAction)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor blackColor]];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor blackColor]];
}


//navigationBar点击事件
- (void)locationAction {
    
}

- (void)whereAction {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
